﻿using System;
using System.Web.Profile;
using Medianamik.Core.Profile;

namespace Medianamik.Core
{
    public class Publication
    {
        internal Publication(Guid pPublicationID, DateTime pStartDate, DateTime? pEndDate,
            int pRevision, string pCreatedBy)
        {
            ID = pPublicationID;
            StartDate = pStartDate;
            EndDate = pEndDate;
            Revision = pRevision;
            _createdBy = pCreatedBy;
        }

        public Guid ID { get; private set; }

        public DateTime StartDate { get; private set; }

        public DateTime? EndDate { get; private set; }

        public virtual bool Equals(Publication other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;

            return other.ID.Equals(ID);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof(Publication)) return false;

            return Equals((Publication)obj);
        }

        public override int GetHashCode()
        {
            return ID.GetHashCode();
        }

        public int Revision { get; private set; }

        private readonly string _createdBy;
        private string _createdByResolved;

        public string CreatedBy
        {
            get
            {
                if (_createdByResolved == null && !String.IsNullOrEmpty(_createdBy))
                {
                    var profile = (UserProfile) ProfileBase.Create(_createdBy);
                    if (profile != null && !String.IsNullOrEmpty(profile.FullName))
                        _createdByResolved = profile.FullName;
                    else
                        _createdByResolved = "";
                }

                return _createdByResolved;
            }
        }
    }
}